//
//  MPBModel+MerageFunc.m
//  MiaoPai
//
//  Created by jiaxuzhou on 2017/8/14.
//  Copyright © 2017年 Jeakin. All rights reserved.
//

#import "MPBModel+MerageFunc.h"
#import <objc/runtime.h>
static void *MPTModelCachedPropertyKeysKey = &MPTModelCachedPropertyKeysKey;

@implementation MPBModel (MerageFunc)

- (void)mergeValuesForKeysFromModel:(MPBModel *)model
{
    NSMutableArray *propertyKeys = [model.class propertyKeys];
    for (NSString *key in propertyKeys)
    {
        if (![propertyKeys containsObject:key]) continue;
        
        [self mergeValueForKey:key fromModel:model];
    }
}

- (void)mergeValueForKey:(NSString *)key fromModel:(MPBModel *)model
{
    NSParameterAssert(key != nil);
    
    SEL selector = MPTSelectorWithCapitalizedKeyPattern("merge", key, "FromModel:");
    if (![self respondsToSelector:selector])
    {
        if (model != nil)
        {
            [self setValue:[model valueForKey:key] forKey:key];
        }
        
        return;
    }
    
    IMP imp = [self methodForSelector:selector];
    void (*function)(id, SEL, MPBModel *) = (__typeof__(function))imp;
    function(self, selector, model);
}


SEL MPTSelectorWithKeyPattern(NSString *key, const char *suffix) {
    NSUInteger keyLength = [key maximumLengthOfBytesUsingEncoding:NSUTF8StringEncoding];
    NSUInteger suffixLength = strlen(suffix);
    
    char selector[keyLength + suffixLength + 1];
    
    BOOL success = [key getBytes:selector maxLength:keyLength usedLength:&keyLength encoding:NSUTF8StringEncoding options:0 range:NSMakeRange(0, key.length) remainingRange:NULL];
    if (!success) return NULL;
    
    memcpy(selector + keyLength, suffix, suffixLength);
    selector[keyLength + suffixLength] = '\0';
    
    return sel_registerName(selector);
}

SEL MPTSelectorWithCapitalizedKeyPattern(const char *prefix, NSString *key, const char *suffix)
{
    NSUInteger prefixLength = strlen(prefix);
    NSUInteger suffixLength = strlen(suffix);
    
    NSString *initial = [key substringToIndex:1].uppercaseString;
    NSUInteger initialLength = [initial maximumLengthOfBytesUsingEncoding:NSUTF8StringEncoding];
    
    NSString *rest = [key substringFromIndex:1];
    NSUInteger restLength = [rest maximumLengthOfBytesUsingEncoding:NSUTF8StringEncoding];
    
    char selector[prefixLength + initialLength + restLength + suffixLength + 1];
    memcpy(selector, prefix, prefixLength);
    
    BOOL success = [initial getBytes:selector + prefixLength maxLength:initialLength usedLength:&initialLength encoding:NSUTF8StringEncoding options:0 range:NSMakeRange(0, initial.length) remainingRange:NULL];
    if (!success) return NULL;
    
    success = [rest getBytes:selector + prefixLength + initialLength maxLength:restLength usedLength:&restLength encoding:NSUTF8StringEncoding options:0 range:NSMakeRange(0, rest.length) remainingRange:NULL];
    if (!success) return NULL;
    
    memcpy(selector + prefixLength + initialLength + restLength, suffix, suffixLength);
    selector[prefixLength + initialLength + restLength + suffixLength] = '\0';
    
    return sel_registerName(selector);
}

void mpt_ext_executeCleanupBlock (__strong mpt_ext_cleanupBlock_t *block)
{
    (*block)();
}


@end
